package kt.com.bilin.coin.ui.activity

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ComponentActivity
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelProvider.NewInstanceFactory
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.launcher.ARouter
import com.umeng.analytics.MobclickAgent
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kt.com.bilin.coin.api.ApiObserver
import kt.com.bilin.coin.api.ApiRequest
import kt.com.bilin.coin.listener.OnStartActivityListener
import kt.com.bilin.coin.viewmodel.BaseViewModel
import org.greenrobot.eventbus.EventBus

fun <T> ComponentActivity.launchUI(
        block: suspend CoroutineScope.() -> T, apiObserver: ApiObserver<T>? = null, error: ((Throwable) -> Unit)? = null, complete: (() -> Unit)? = null,
): Job = ApiRequest(lifecycleScope).launchUI(block, error, complete, apiObserver)

open class BaseActivity(private val layoutResID: Int? = null) : AppCompatActivity(), OnStartActivityListener,
        View.OnClickListener {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        com.yanzhenjie.sofia.Utils.setStatusBarDarkFont(this, true)
        ARouter.getInstance().inject(this)
        if (layoutResID != null) {
            setContentView(layoutResID)
        } else {
            initView()
            initData()
        }
    }

    override fun setContentView(layoutResID: Int) {
        super.setContentView(layoutResID)
        initView()
        initData()
    }

    open fun initView() {}
    open fun initData() {}

    override fun moveTaskToBack(nonRoot: Boolean): Boolean {
        return super.moveTaskToBack(true)
    }

    open fun <T : BaseViewModel> createViewModel(clz: Class<T>): T {
        return ViewModelProvider(this, NewInstanceFactory()).get(clz)
    }

    open fun doFinish() {
        finish()
    }

    override fun onStart() {
        super.onStart()
        kotlin.runCatching {
            if (!EventBus.getDefault().isRegistered(this)) {
                EventBus.getDefault().register(this)
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }

    override fun onResume() {
        super.onResume()
        MobclickAgent.onResume(this)
    }

    override fun onPause() {
        MobclickAgent.onPause(this)
        super.onPause()
    }

    override fun goActicity(clazz: Class<*>, bundle: Bundle?, requestCode: Int?) {
        val intent = Intent(this, clazz)
        bundle?.let { intent.putExtras(it) }
        if (requestCode != null) {
            startActivityForResult(intent, requestCode)
        } else {
            startActivity(intent)
        }
    }

    override fun goActicityFinish(clazz: Class<*>, bundle: Bundle?, requestCode: Int?) {
        goActicity(clazz, bundle, requestCode)
        doFinish()
    }

    override fun onClick(v: View?) {

    }


}